package com.hmeter.mongodb.gui;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.mongodb.MongoDbUpdateSampler;

public class MongoDbUpdateSamplerGui extends AbstractSamplerGui {

	/**
	 * 
	 */
	private static final long serialVersionUID = 522709729259732877L;
	
	private JLabeledTextField hostTextField = null;
	private JLabeledTextField portTextField = null;
	private JLabeledTextField dbNameTextField = null;
	private JLabeledTextField collectionNameTextField = null;
	private JLabeledTextField queryListTextField = null;
	private JLabeledTextField operatorTextField = null;
	private JLabeledTextField delimiterTextField = null;
	private JLabeledTextField updateValueTextField = null;
	
	public MongoDbUpdateSamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
        
        hostTextField = new JLabeledTextField("Host");
        portTextField = new JLabeledTextField("Port");
        dbNameTextField = new JLabeledTextField("MongoDB name");
        collectionNameTextField = new JLabeledTextField("Collection name");
        queryListTextField = new JLabeledTextField("List of queries (format: key|operator|value, $eq if operator=equals)");
        operatorTextField = new JLabeledTextField("Sum operator");
        delimiterTextField = new JLabeledTextField("Delimiter");
        updateValueTextField = new JLabeledTextField("Update content (format: key:value)");
        
        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        
        //input data panel
        VerticalPanel inputPanel = new VerticalPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input data"));
        inputPanel.add(hostTextField);
        inputPanel.add(portTextField);
        inputPanel.add(dbNameTextField);
        inputPanel.add(collectionNameTextField);

        //update document panel
        VerticalPanel updateDocumentPanel = new VerticalPanel();
        updateDocumentPanel.setBorder(BorderFactory.createTitledBorder("Update document by clause"));
        updateDocumentPanel.add(queryListTextField);
        updateDocumentPanel.add(delimiterTextField);
        updateDocumentPanel.add(operatorTextField);
        updateDocumentPanel.add(updateValueTextField);
        
        panel.add(inputPanel);
        panel.add(updateDocumentPanel);
        
        add(panel, BorderLayout.CENTER);
	}

	@Override
	public void clearGui()
	{
		super.clearGui();
		hostTextField.setText("");
		portTextField.setText("");
		dbNameTextField.setText("");
		collectionNameTextField.setText("");
		queryListTextField.setText("");
		operatorTextField.setText("");
		delimiterTextField.setText("");
		updateValueTextField.setText("");
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "MongoDB Update Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		MongoDbUpdateSampler mongoDbUpdateSampler = new MongoDbUpdateSampler();
		modifyTestElement((TestElement) mongoDbUpdateSampler);
		return (TestElement) mongoDbUpdateSampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof MongoDbUpdateSampler)
		{
			MongoDbUpdateSampler mongoDbUpdateSampler = (MongoDbUpdateSampler) element;
			mongoDbUpdateSampler.setHost(hostTextField.getText());
			mongoDbUpdateSampler.setPort(portTextField.getText());
			mongoDbUpdateSampler.setDbName(dbNameTextField.getText());
			mongoDbUpdateSampler.setCollectionName(collectionNameTextField.getText());
			mongoDbUpdateSampler.setQueryList(queryListTextField.getText());
			mongoDbUpdateSampler.setOperator(operatorTextField.getText());
			mongoDbUpdateSampler.setDelimiter(delimiterTextField.getText());
			mongoDbUpdateSampler.setUpdateValue(updateValueTextField.getText());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof MongoDbUpdateSampler)
		{
			MongoDbUpdateSampler mongoDbUpdateSampler = (MongoDbUpdateSampler) element;
			hostTextField.setText(mongoDbUpdateSampler.getHost());
			portTextField.setText(mongoDbUpdateSampler.getPort());
			dbNameTextField.setText(mongoDbUpdateSampler.getDbName());
			collectionNameTextField.setText(mongoDbUpdateSampler.getCollectionName());
			queryListTextField.setText(mongoDbUpdateSampler.getQueryList());
			operatorTextField.setText(mongoDbUpdateSampler.getOperator());
			delimiterTextField.setText(mongoDbUpdateSampler.getDelimiter());
			updateValueTextField.setText(mongoDbUpdateSampler.getUpdateValue());
		}
	}
}
